<template>
	<view>
		<view class="line">
			<text style="font-weight: bold;">合同列表</text>
			<navigator :url="'htedit?gcname=' + title + '&gcid=' + gcid">添加合同</navigator>
		</view>
		<view class="line">
			<view>合同名</view><view>金额</view><view>开票额</view><view>付款额</view><view>操作</view>
		</view>
		<view class="line" v-for="(item,index) of htlist" v-bind:key="item.id">
			<template v-if="item.tp < 4">
				<view><navigator :url="'/pages/htdetail?id='+item.id">[{{htType[item.tp]}}]{{item.names}}</navigator></view>
				<view>{{(item.amount||0)/100}}</view>
				<view>{{(item.kpv||0)/100}}</view>
				<view>{{(-item.fkv||0)/100}}</view>
				<view class="lineopt">
					<button @click="clkkp(item,1)">开票</button>
					<button @click="clkkp(item,2)">付款</button>
					<button @click="clklist(item)">列表</button>
					<navigator :url="'/pages/htedit?id='+item.id">编辑</navigator>
					<button @click="clkhtdel(item.id)">删除</button>
				</view>				
			</template>
			<template v-if="item.tp > 3">
				<view>{{item.names}}</view>
				<view>{{(item.amount||0)/100}}</view>
				<view>{{(item.kpv||0)/100}}</view>
				<view>{{(-item.fkv||0)/100}}</view>
				<view class="lineopt"></view>
			</template>
		</view>
		<kpedit v-if="kpshow" :htid="htid" :kpid="kpid" :tp="kptp" :htname="htname" v-on:close="kpclose"></kpedit>
		<kplist v-if="htshow" :htid="htid" :htname="htname" v-on:close="htclose"></kplist>
	</view>
</template>

<script>
	import kpedit from "@/components/kpedit.vue";
	import kplist from "@/components/kplist.vue";
	export default {
		components:{kpedit,kplist},
		data() {
			return {
				title:"",
				gcid:0,
				htlist:[],
				hzlist:[],
				kpshow:false,
				kpid:0,
				kptp:1,
				htshow:false,
				htname:'',
				htid:0
			}
		},
		onShow(){
			let gid = uni.getStorageSync("gcid");
			if(gid != this.gcid){
				this.gcid = gid;
				this.gcdetail();
				//console.log("onshow", this.gcid)
			}
			this.getht();
		},
		onLoad(opt) {
			this.gcid = opt.id;
			if(!this.gcid){
				this.gcid = uni.getStorageSync("gcid");
				console.log("gcdetail", uni.getStorageSync("gcid"))
			}
			this.gcdetail();
			this.getht();
		},
		methods: {
			gcdetail(){
				let self = this;
				uni.request({
					url:this.apibase + "gcdetail?id=" + this.gcid,
					success: (res) => {
						self.title = res.data.names;
						uni.setNavigationBarTitle({
						    title: self.title
						});
					}
				});
			},
			getht(){
				let self = this;
				uni.request({
					url:this.apibase + "htlist?gcid=" + this.gcid,
					success: (res) => {
						self.hzlist = [];
						self.htlist = res.data;
						for(let it of self.htlist){
							if(!self.hzlist[it.tp]) self.hzlist[it.tp] = {names:"汇总",tp:it.tp+4,amount:0,kpv:0,fkv:0};
							self.hzlist[it.tp].amount += it.amount;
							self.hzlist[it.tp].kpv += it.kpv;
							self.hzlist[it.tp].fkv += it.fkv;
						}
						//console.log(self.hzlist);
						let otp = -1;
						for(let i=0;i<self.htlist.length;i++){
							if(otp != self.htlist[i].tp){
								if(otp != -1){
									self.htlist.splice(i,0,self.hzlist[self.htlist[i].tp]);
									i++;
								}
							}
							otp = self.htlist[i].tp;
							//console.log(i, otp);
						}
						if(otp > -1){
							self.htlist.push(self.hzlist[otp]);
						}
						//console.log(otp,self.htlist);
					}
				});
			},
			clkkp(e,tp){
				//console.log("clkkp",e);
				this.kpshow = true;
				this.kpid = 0;
				this.kptp = tp;
				this.htid = e.id;
				this.htname = e.names;
			},
			kpclose(){
				this.kpshow = false;
				this.getht();
			},
			clklist(e){
				//console.log("clkkp",e);
				this.htshow = true;
				this.htid = e.id;
				this.htname = e.names;
			},
			htclose(){
				this.htshow = false;
				this.getht();
			},
			clkhtdel(id){
				let self = this;
				uni.request({
					url:this.apibase + "htdel?htid=" + id,
					success: (res) => {
						if(res.data.code){
							uni.showToast({
								title:res.data.msg
							})
						}else{
							self.getht();
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
